package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsStorage;
import com.qf.service.DtsStorageService;
import com.qf.util.ResponseUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;


@RequestMapping("/admin/storage")
@CrossOrigin
@RestController
public class AdminStorageController {

    @Autowired
    private DtsStorageService dtsStorageService;

    /**
     * 系统管理
     * 查询对象列表
     * @param page
     * @param limit
     * @param key
     * @param name
     * @param sort
     * @param order
     * @return
     */
    @GetMapping("/list")
    @RequiresPermissions("admin:storage:list")
    @RequiresPermissionsDesc(menu = "系统管理,对象存储",button = "对象列表")
    public Object storageList(@RequestParam(defaultValue = "1") Integer page,
                              @RequestParam(defaultValue = "20") Integer limit,
                              String key,String name,String sort,String order){

        IPage<DtsStorage> iPage = dtsStorageService.selectStorageList(page,limit,key,name,sort,order);
        long total = iPage.getTotal();
        List<DtsStorage> dts = iPage.getRecords();

        DtsStorage [] dtsStorages = new DtsStorage[dts.size()];

        for (int i = 0; i < dts.size(); i++) {
            dtsStorages[i] = dts.get(i);
        }

        HashMap<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("items", dtsStorages);
        return ResponseUtil.ok(data);

    }


    //上传保存
    @PostMapping("/create")
    @RequiresPermissions("admin:storage:create")
    @RequiresPermissionsDesc(menu = "系统管理，对象存储",button = "上传")
    public Object storageCreate(@RequestParam("file")MultipartFile file) throws Exception{

        //获取文件名
        String filename = file.getOriginalFilename();
        //判空
        if (StringUtils.isEmpty(filename)){
            throw new RuntimeException("上传的文件为空，不允许上传");
        }

        //截取文件扩展名
        String ext = filename.substring(filename.lastIndexOf("."));

        DtsStorage storage = dtsStorageService.add(filename, (int) file.getSize(),ext,file.getBytes());

        return ResponseUtil.ok(storage);
    }


    //更新 系统管理  对象存储
    @RequiresPermissions("admin:storage:update")
    @RequiresPermissionsDesc(menu = "系统管理，对象存储",button = "编辑")
    @RequestMapping("/update")
    public Object storageUpdate(@RequestBody DtsStorage dtsStorage){

        Integer result =  dtsStorageService.update(dtsStorage);

        if (result == 0){
            return ResponseUtil.updatedDataFailed();
        }
        return ResponseUtil.ok(dtsStorage);
    }

    //删除对象  逻辑删除
    @RequiresPermissions("admin:storage:delete")
    @RequiresPermissionsDesc(menu = "系统管理，对象存储",button = "删除")
    @RequestMapping("/delete")
    public Object storageDelete(@RequestBody DtsStorage dtsStorage){
        DtsStorage dtsStorage1 = new DtsStorage();
        dtsStorage1.setDeleted(true);
        dtsStorage1.setId(dtsStorage.getId());
        Integer update = dtsStorageService.update(dtsStorage1);
        if (update == 0){
            return ResponseUtil.updatedDataFailed();
        }
        return ResponseUtil.ok();
    }

}
